#ifndef CORE_H
#define CORE_H
#include <vector>
#include <QSize>
#include <QPoint>
#include "exceptions.hpp"


using namespace std;


template< class T >
class Core
{
public:

	Core () : _size (1, 1), _center (), _data (1, 1.0)
	{
	
	}

	Core (QSize size) : _size (size), 
			_center (size.width () / 2, size.height () / 2), 
			_data (size.width () * size.height ())
	{
		if (size == QSize (0, 0))
			OOPTHROW (CoreEmptyException);
	}

	Core (QSize size, QPoint center) : _size (size), 
		_center (center), _data (size.width () * size.height ())
	{
		if (size == QSize (0, 0))
			OOPTHROW (CoreEmptyException);
		if (!QRect (QPoint (), size).contains (center))
			OOPTHROW (CoreCenterOutOfBoundaryException);
	}

	Core (const Core & right) : _data (right._data), 
		_size (right._size), _center (right._center)
	{

	}

	~Core() {}

	Core & operator= (const Core & right)
	{
		_data = right._data;
		_size = right._size;
		_center = right._center;
		return *this;
	}

	const T & operator () (int x, int y) const
	{
		return _data[y * _size.width () + x];
	}

	T & operator () (int x, int y)
	{
		return _data[y * _size.width () + x];
	}

	void set (int x, int y, const T & elem)
	{
		_data[y * _size.width () + x] = elem;
	}

	QSize getSize () const
	{
		return _size;
	}

	QPoint getCenter () const
	{
		return _center;
	}

	void setCenter (const QPoint & center)
	{
		if (!QRect (QPoint (), _size).contains (center))
			OOPTHROW (CoreCenterOutOfBoundaryException);
		_center = center;
	}

private:

	vector< T > _data;
	QSize _size;
	QPoint _center;

};

#endif // CORE_H
